package com.example.demo.repository;

import com.example.demo.entity.Info;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface InfoRepository extends JpaRepository<Info,Integer> {
    @Query("select t from Info t where t.title=:title  and t._live=:_live")
    Info findByTitle(@Param("title") String title, @Param("_live") Integer _live);

    @Query("select t from Info t where t.id=:id and t._live=:_live")
    Info findBy_live(@Param("id") Integer id, @Param("_live") Integer _live);

    @Query(value = "select t.id,t.title,t._live,t.createtime,t.details from Info t where t._live=1 order by t.createtime desc limit :page,:pageSize",nativeQuery = true)
    List<Info> findAllInfo(@Param("page")Integer page,@Param("pageSize")Integer pageSize);

    @Query(value = "select count(u.id) from (select t.id,t.title,t._live,t.createtime,t.details from Info t where t._live=1 order by t.createtime)as u",nativeQuery = true)
    int total();
}
